package com.xiaobaibai.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.xiaobaibai.entity.UserAddress;
import com.xiaobaibai.response.ResponseFormat;
import com.xiaobaibai.service.IUserAddressService;
import com.xiaobaibai.units.SecurityUnit;
import com.xiaobaibai.valid.AddGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "地址相关接口")
@RequestMapping("/address")
public class UserAddressController {

    @Autowired
    private IUserAddressService userAddressService;

    @GetMapping("/list")
    @ApiOperation("获取一个用户的地址列表")
    public ResponseFormat getAddressListByUserId(){
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        Integer userId= (Integer) authentication.getPrincipal();
        //用户地址也可以缓存,后面在用户添加地址时清空缓存即可
        List<UserAddress> list = userAddressService.getAddressByUserId(userId);

        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getFlag()){
                //交换位置
                if(i==0)
                    break;
                else{
                    UserAddress temp=list.get(0);
                    list.set(0,list.get(i));
                    list.set(i,temp);
                    break;
                }
            }
        }

        return ResponseFormat.ok("获取用户地址成功",list);
    }

    @PostMapping("/add")
    @ApiOperation("添加用户地址")
    public ResponseFormat addAddress(@RequestBody @Validated(value = AddGroup.class)
                                                 UserAddress userAddress){
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        userAddress.setUserId((Integer) authentication.getPrincipal());
        userAddress.setFlag(false);
        boolean flag = userAddressService.addUserAddress(userAddress);
        if(!flag){
            return ResponseFormat.failure("地址添加失败");
        }
        return ResponseFormat.ok("地址添加成功");
    }

    @PutMapping("/update")
    @ApiOperation("修改地址")
    public ResponseFormat updateAddress(@RequestBody UserAddress userAddress){
        Integer userId = SecurityUnit.getUserId();
        if(!userId.equals(userAddress.getUserId()))
            return ResponseFormat.failure("不可横向越权");
        if(!userAddressService.updateById(userAddress))
            return ResponseFormat.failure("修改失败");
        return ResponseFormat.ok("修改成功");
    }

    @PutMapping("/updateDefault")
    @ApiOperation("设置默认地址")
    public ResponseFormat updateDefault(@RequestBody Integer addressId){
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        Integer userId= (Integer) authentication.getPrincipal();
        UpdateWrapper<UserAddress> userAddressUpdateWrapper=new UpdateWrapper<>();
        userAddressUpdateWrapper.eq("user_id",userId);
        userAddressUpdateWrapper.eq("flag",1);
        userAddressUpdateWrapper.set("flag",0);
        userAddressService.updateAddressDefault(userAddressUpdateWrapper,userId);

        UpdateWrapper<UserAddress> u2=new UpdateWrapper<>();
        u2.eq("user_id",userId);
        u2.eq("id",addressId);
        u2.set("flag",1);
        userAddressService.updateAddressDefault(u2,userId);

        return ResponseFormat.ok("用户默认地址修改成功");
    }

}
